package com.wxd.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wxd.service.CarMapService;
import com.wxd.service.impl.CarMapServiceImpl;

public class CarMapAction extends ActionSupport{
	private CarMapService carMapService = new CarMapServiceImpl();
	private InputStream inputStream;
	public InputStream getResult(){
		return this.inputStream;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String getDevGroupList() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String devName = request.getParameter("devName");
		String devType = request.getParameter("devType");
		String devUnit = request.getParameter("devUnit");
		
		if(StringUtils.isEmpty(devName)){
			devName = null;
		}
		if(StringUtils.isEmpty(devType)){
			devType = null;
		}
		if(StringUtils.isEmpty(devUnit)){
			devUnit = null;
		}
		
		JSONArray array = carMapService.getDevGroupList(devName,devType,devUnit);
		
		JSONObject object = new JSONObject();
		object.put("items", array);
		
		this.inputStream = new ByteArrayInputStream(object.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	public String getDevListForXunHang() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String devName = request.getParameter("devName");
		String devType = request.getParameter("devType");
		String devUnit = request.getParameter("devUnit");
		
		if(StringUtils.isEmpty(devName)){
			devName = null;
		}
		if(StringUtils.isEmpty(devType)){
			devType = null;
		}
		if(StringUtils.isEmpty(devUnit)){
			devUnit = null;
		}
		JSONArray array = carMapService.getDevGroupList(devName,devType,devUnit);
		
		JSONArray resultArray = new JSONArray();
		for(int n=0;n<array.size();n++){
			JSONObject jo = (JSONObject)array.get(n);
			resultArray.add(jo.get("devNum").toString());
		}
		this.inputStream = new ByteArrayInputStream(resultArray.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	
	//根据车牌号获得车辆的最近行驶记录信息
	public String getRecentDevInfoByDevNum() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String devNum = request.getParameter("devNum");
		JSONArray result = null;
		if(!StringUtils.isEmpty(devNum)){
			result = carMapService.getRecentDevInfoByDevNum(devNum);
		}else{
			result = new JSONArray();
		}
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	
	//根据车牌号汇总今天信息
	public String getTodaySummaryByDevNum() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String devNum = request.getParameter("devNum");
		JSONArray result = null;
		if(!StringUtils.isEmpty(devNum)){
			result = carMapService.getTodaySummaryByDevNum(devNum);
		}else{
			result = new JSONArray();
		}
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	//获得给定设备的设备编号查找设备曾经经过的坐标点
	public String getDevPointsByDevNum() throws Exception{
		JSONArray result = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String devNum = request.getParameter("devNum");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtils.isEmpty(devNum)||StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
			result = new JSONArray();
		}else{
			result = carMapService.getDevPointsByDevNum(devNum,startTime,endTime);
		}
		
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		
		return SUCCESS;
	}
}
